# @Time    : 2022/2/2 19:14
# @Author  : junbaibai
# @File    : save_node.py
import pathlib
from datetime import datetime


class SaveNode:
    table_name = "save_node"
    __slots__ = ["id", "parent", "src_path", "file_path", "update_time",
                 "player", "ideology", "date", "difficulty", "version", "ironman"]

    def __init__(self, values: tuple = None, node: 'SaveNode' = None):
        if values is not None:
            val_len = values.__len__()
            for index in range(self.__slots__.__len__()):
                key = self.__slots__[index]
                value = values[index] if index < val_len else None
                setattr(self, key, value)
        elif node is not None:
            for key in self.__slots__:
                setattr(self, key, getattr(node, key))
        else:
            for key in self.__slots__:
                setattr(self, key, None)
            self.update_time = datetime.now().strftime('%Y%m%d%H%M%S')

    def init_update_time(self):
        self.update_time = datetime.now().strftime('%Y%m%d%H%M%S')

    def init_file_path(self):
        obj = self
        base_dir = f"./save_games/{pathlib.Path(obj.src_path).name}"
        obj.file_path = f"{base_dir}/{obj.date}_{obj.update_time}.7z"

    @property
    def values(self):
        return (getattr(self, i) for i in self.__slots__)

    @property
    def init_field(self):
        return self.__slots__[1:5]

    @property
    def init_field_value(self):
        return (getattr(self, i) for i in self.init_field)

    @property
    def normal_field(self):
        return self.__slots__[1:]

    @property
    def normal_field_value(self):
        return (getattr(self, i) for i in self.normal_field)

    @property
    def hoi_field(self):
        return self.__slots__[5:]

    @property
    def hoi_field_value(self):
        return (getattr(self, i) for i in self.hoi_field)
